<template>
  <div class="channel-edit">
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <van-button
        type="danger"
        plain
        round
        size="mini"
        class="edit-btn"
        @click="isEdit = !isEdit"
        >{{ isEdit ? '完成' : '编辑' }}</van-button
      >
    </van-cell>
    <van-grid :gutter="10" class="my-grid">
      <van-grid-item
        class="grid-item"
        v-for="(channel, index) in myChannels"
        :key="index"
        border="false"
        @click="onMyChannelClick(channel, index)"
      >
        <!-- includes 包含的返回false 取反 -->
        <van-icon
          v-show="isEdit && !felxChannels.includes(channel.id)"
          slot="icon"
          name="clear"
        ></van-icon>
        <span slot="text" class="text" :class="{ active: index === active }">{{
          channel.name
        }}</span>
      </van-grid-item>
    </van-grid>
    <van-cell :border="false">
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>
    <van-grid :gutter="10" class="recommend-grid">
      <van-grid-item
        class="grid-item"
        icon="plus"
        v-for="(channel, index) in recommendChannels"
        :key="index"
        :text="channel.name"
        border="false"
        @click="onAddChannel(channel)"
      />
    </van-grid>
  </div>
</template>

<script>
import { getChannel, addUserChannel, deleteUserChannel } from '@/api/channel'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage'
export default {
  name: 'ChannelEdit',
  props: {
    myChannels: {
      require: true
    },
    active: {
      require: true
    }
  },
  data () {
    return {
      // 所有频道数据
      allChannels: [],
      isEdit: false,
      felxChannels: [0]
    }
  },
  computed: {
    ...mapState(['user']),
    // eslint-disable-next-line vue/return-in-computed-property
    recommendChannels () {
      return this.allChannels.filter(channel => {
        return !this.myChannels.find(myChannels => {
          return myChannels.id === channel.id
        })
      })
    }
    //   const channels = []
    //   this.allChannels.forEach(channel => {
    //     const ret = this.myChannels.find(myChannels => {
    //       return myChannels.id === channel.id
    //     })
    //     if (!ret) {
    //       channels.push(channel)
    //     }
    //   })
    //   return channels
  },
  created () {
    this.lodaChannel()
  },
  methods: {
    async lodaChannel () {
      try {
        const { data } = await getChannel()
        this.allChannels = data.data.channels
        console.log(this.allChannels, 222)
      } catch (err) {
        this.$toast('数据获取失败')
      }
    },
    // 添加频道
    async onAddChannel (channel) {
      this.myChannels.push(channel)
      if (this.user) {
        try {
          const { data } = await addUserChannel({
            id: channel.id,
            seq: this.myChannels.length
          })
          console.log(data, 111)
        } catch (err) {
          this.$toast('保存失败，请稍后重试')
        }
      } else {
        setItem('TOUTIAO_CHANNELS', this.myChannels)
      }
    },
    // 删除频道
    onMyChannelClick (channel, index) {
      if (this.isEdit) {
        if (this.felxChannels.includes(channel.id)) {
          return
        }
        if (index <= this.active) {
          this.$emit('update-active', this.active - 1)
        }
        this.myChannels.splice(index, 1)
        this.deleteChannel(channel)
      } else {
        this.$emit('update-active', index, false)
      }
    },
    async deleteChannel (channel) {
      try {
        if (this.user) {
          await deleteUserChannel(channel.id)
        } else {
          setItem('TOUTIAO_CHANNELS', this.myChannels)
        }
      } catch (err) {
        this.$toast('操作失败，请稍后重试')
      }
    }
  }

}
</script>

<style lang="less" scoped>
.channel-edit {
  padding-top: 85px;
  .title-text {
    font-size: 32px;
    color: #333333;
  }
  /deep/ .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: solid 0.1px #f85959 !important;
  }
  /deep/ [class*='van-hairline']::after {
    border: none;
  }
  /deep/.van-cell::after {
    border: none;
  }
  .grid-item {
    width: 160px;
    height: 86px;
    /deep/ .van-grid-item__content {
      white-space: nowrap;
      background-color: #f4f5f6;
      .van-grid-item__text,
      .text {
        font-size: 28px;
        color: #222;
        margin-top: 0;
      }
      .active {
        color: red;
      }
    }
  }
  /deep/ .recommend-grid {
    .grid-item {
      .van-grid-item__content {
        flex-direction: row;
        .van-icon {
          font-size: 28px;
          margin-right: 6px;
        }
      }
    }
  }
  /deep/ .my-grid {
    .grid-item {
      .van-icon-clear {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
  }
}
</style>
